package chapter15;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-20 下午12:01
 */
@Controller
@RequestMapping("/file")
public class UploadController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("uploadForm");
        return mv;
    }

    @RequestMapping("/index2")
    public ModelAndView index2() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("uploadFile2");
        return mv;
    }

    @RequestMapping("/index3")
    public ModelAndView index3() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("uploadFile3");
        return mv;
    }

    /*使用HttpServletRequest*/
    @RequestMapping("/upload")
    public ModelAndView upload(HttpServletRequest request) {
        //进行转化
        MultipartHttpServletRequest mhsr = (MultipartHttpServletRequest) request;
        //获取上传的文件
        MultipartFile file = mhsr.getFile("file");
        //设置视图为JSON视图
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        //获取文件原始名称
        String fileName = file.getOriginalFilename();
        //目标文件
        File dest = new File(fileName);
        try {
            //保存文件
            file.transferTo(dest);
            //保存成功
            mv.addObject("success", true);
            mv.addObject("msg", "上传文件成功");
        } catch (IOException e) {
            //保存失败
            mv.addObject("success", false);
            mv.addObject("msg", "上传文件失败");
            LOGGER.error(e.toString());
        }
        return mv;
    }

    /*使用multipartFile*/
    @RequestMapping("/uploadMultipartFile")
    public ModelAndView uploadMultipartFile(MultipartFile file) {
        //设置视图为JSON视图
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        //获取文件原始名称
        String fileName = file.getOriginalFilename();
        //目标文件
        File dest = new File(fileName);
        try {
            //保存文件
            file.transferTo(dest);
            //保存成功
            mv.addObject("success", true);
            mv.addObject("msg", "上传文件成功");
        } catch (IOException e) {
            //保存失败
            mv.addObject("success", false);
            mv.addObject("msg", "上传文件失败");
            LOGGER.error(e.toString());
        }
        return mv;
    }

    /* 使用Part */
    @RequestMapping("/uploadPart")
    public ModelAndView uploadPart(Part file) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        //获取文件原始名称
        String fileName = file.getSubmittedFileName();
        try {
            //保存文件
            file.write("/home/lzy/mvc/uploads/" + fileName);
            //保存成功
            mv.addObject("success", true);
            mv.addObject("msg", "上传文件成功");
        } catch (IOException e) {
            //保存失败
            mv.addObject("success", false);
            mv.addObject("msg", "上传文件失败");
            LOGGER.error(e.toString());
        }
        return mv;
    }
}
